//
//  JXLVisitorView.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/9/21.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit
protocol JXLVisitorViewDelegate: NSObjectProtocol {
    func didSelectedLogin()
}

class JXLVisitorView: UIView {

    //定义登录按钮的闭包
    var loginCloser: (() -> ())?
    
    //定义代理
    weak var delegate: JXLVisitorViewDelegate?
    // MARK: 懒加载子控件
    //旋转视图
   private lazy var cycleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩视图
    private lazy var maskImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //  主页
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //  未登录信息视图
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        //  居中
        label.textAlignment = .center
        //  多行显示
        label.numberOfLines = 0
        return label
        
    }()

    //  登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //  去掉点击的高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    //  注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //  去掉点击的高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()

    
    //MARK: 方法＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
    
    //重载init方法，布局UI
   override init(frame: CGRect) {
        super.init(frame: frame)
    
    setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //布局子控件方法
    private func setupUI() {
        //设置背景颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        //添加子视图
        addSubview(cycleImageView)
         addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)

        //布局子控件
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
        }

        
        
//        //使用系统自动布局
//        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//        //  设置布局方式
//                addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
//        
//                addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))

        
    }
    //修改访客视图信息
    func updatevisitorInfo(message: String?, imageName: String?) -> Void {
        if let message = message, let imageName = imageName  {
            messageLabel.text = message
            iconImageView.image = UIImage(named: imageName)
            cycleImageView.isHidden = true
        } else {
            //  代码执行到此,表示首页
            //  执行旋转动画
            startAnimation()
        }
        
    }
    
    //开始动画
    private func startAnimation() {
        //创建核心动画对象
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //执行到目的地
        animation.toValue = 2 * M_PI
        //重复次数
        animation.repeatCount = MAXFLOAT
        //执行动画时长
        animation.duration = 20
        //失去焦点让其不释放
        animation.isRemovedOnCompletion = false
        
        //给cycleView添加动画
        cycleImageView.layer.add(animation, forKey: nil)
    }
    
    //点击登录
   @objc private func loginAction() {
    print("点击登录")
    //执行登录闭包
    loginCloser?()
    //执行代理方法
    delegate?.didSelectedLogin()
    }
}
